<?php

namespace App\Http\Controllers\Api\Agent;

use App\Http\Controllers\Controller;
use App\Http\Resources\Agent\AgentResource;
use App\Http\Resources\Agent\AgentSliderCollection;
use App\Http\Resources\Agent\CityCollection;
use App\Http\Resources\Agent\WorkshopResource;
use App\Http\Resources\CommentCollection;
use App\Http\Resources\WorkShopCollection;
use App\Interfaces\agent\AgentInterFace;
use App\model\agent\Agent;
use App\model\agent\AgentSlider;
use App\model\agent\AgentTousu;
use App\model\agent\AgentUser;
use App\model\agent\AgentYuyue;
use App\model\Area;
use App\model\City;
use App\model\Workshop;
use App\model\WorkshopHangye;
use App\model\WorkshopIndustry;
use App\model\WorkshopPurpose;
use App\model\WorkshopTag;
use App\Repository\WorkShopRepository;
use http\Env\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class AgentController extends Controller
{
    protected $agentInterFace;
    public function __construct(AgentInterFace $agentInterFace)
    {
        $this->agentInterFace=$agentInterFace;
    }
    public function login(Request $request){
        $app=app('mini.agent');
        $result=$app->auth->session($request->code);
        $agentUser=   AgentUser::whereOpenid($result->openid)->first();
        if (!$agentUser) {
            $agentUser=new AgentUser();
            $agentUser->openid=$result->openid;
        }
        $agentUser->session_key=$result->session_key;
        $agentUser->save();
        return \response()->json([
            'code'=>0,
            'message'=>'成功',
            'data'=>$agentUser->toArray()
        ]);

    }
    /**
     * 客户详情
     * @param $id
     * @return AgentResource|\Illuminate\Http\JsonResponse
     */
    public function agent($id){

        $agent=$this->agentInterFace->agent($id);

        if ($agent&&$agent->status==Agent::STATUS_DISPLAY){
            return new AgentResource($agent);
        }
        return \response()->json(
            [
                'code'=>1,
                'message'=>'没有此用户'
            ]
        );
    }

    /**
     * 推荐的厂房
     * @return WorkShopCollection
     */
    public function workshopRecommend(){
        $workshop=$this->agentInterFace->workshopList(10);
        return new WorkShopCollection($workshop);
    }
    /**
     *房源详情
     */
    public function workshop($id){
          $workshop= $this->agentInterFace->workshop($id);
          return new WorkshopResource($workshop);
    }
    /**
     * 房源列表
     * @param Request $request
     * @return WorkShopCollectionw
     */
    public function workshopList(Request $request){
        $workshop=$this->agentInterFace->workshopSearch($request);
        return new WorkShopCollection($workshop);
    }
    public function workshopDetailRecommend(Request $request){
        $workshop=$request->get('workshop');
        $workshop=$this->agentInterFace->workshopList(6);
        return new WorkShopCollection($workshop);
    }

    /**
     * 面积
     */
    public function area(){
       $workshop= Area::all();
       return new CommentCollection($workshop);
    }
    /**
     * 行业
     */
    public function hangye(){
        $workshop= WorkshopHangye::all();
        return new CommentCollection($workshop);
    }
    /**
     * tags
     */
    public function tag(){
        $workshop= WorkshopTag::all();
        return new CommentCollection($workshop);
    }
    /**
     * city
     */
    public function city(){
     $workshop=   City::with('children')
         ->orderBy('order','asc')
         ->whereIn('parent_id',[102,101])
         ->get();
        return $workshop;
    }
    /**
     * 用途
     */
    public function industry(){
      $workshop=  WorkshopIndustry::all();
      return new CommentCollection($workshop);
    }
    public function purpose(){
        $workshop=  WorkshopPurpose::all();
        return new CommentCollection($workshop);
    }
    /**
     *
     */
    public function slider(){
        $slider=AgentSlider::orderBy('sort','desc')->orderBy('id')->get();
        return new AgentSliderCollection($slider);
    }
    /**
     * 预约
     */
    public function yuyue(Request $request){
       $yuyue= new AgentYuyue();
       $yuyue->name=$request->name;
       $yuyue->time=$request->time;
       $yuyue->mobile=$request->mobile;
       $yuyue->workshop_id=$request->workshop_id;
       $yuyue->agent_id=$request->agent_id;
       $yuyue->save();
       event(new \App\Events\AgentYuyue($yuyue));
        return  \response()->json([
           'code'=>0,
           'message'=>'提交成功'
       ]);
    }
    /**
     * 投诉
     */
    public function tousu(Request $request){
        $tousdu=new AgentTousu();
        $tousdu->mobile=$request->mobile;
        $tousdu->name=$request->name;
        $tousdu->desc=$request->desc;
        $tousdu->save();
        return \response()->json(
            [
                'code'=>0,
                'message'=>'投诉成功'
            ]
        );
    }
    public function jiemi(Request $request){
            $iv=$request->iv;
            $data=$request->data;
            $openid=$request->openid;
            $agentUser=AgentUser::whereOpenid($openid)->first();
            $app=app('mini.agent');
            $result=$app->encryptor->decryptData($agentUser->session_key, $iv, $data);
            $agentUser->mobile=$result['purePhoneNumber'];
            $agentUser->save();
            return \response()->json([
            'code'=>0,
            'message'=>'成功',
            'data'=>$agentUser->toArray()
        ]);
    }

    /**
     * 得到海报
     */
    public function workshopQrcode(Request $request,WorkShopRepository $workShopRepository){
        $workshopId=$request->workshop_id;
        $agent_id=$request->agent_id;
        $url=$workShopRepository->miniQrcode(Workshop::find($workshopId),Agent::find($agent_id));
        return \response()->json(
            [
                'code'=>0,
                'data'=>str_replace('http','https',$url)
                ]
        );
    }

    /**
     * 附近的厂房
     * @param Request $request
     * @return WorkShopCollectionw
     */
    public function workshopNearList(Request $request){
        $workshop=$this->agentInterFace->workshopNearList($request);
        return new WorkShopCollection($workshop);
    }
    /**
     * 用户发布的
     * @param Request $request
     * @return WorkShopCollectionw
     */
    public function workshopUserFabu(Request $request){
        $workshop=$this->agentInterFace->workshopUserFabu($request);
        return new WorkShopCollection($workshop);
    }

    /**
     * 用户收藏的
     * @param Request $request
     * @return WorkShopCollectionw
     */
    public function workshopUserStart(Request $request){
        $workshop=$this->agentInterFace->workshopUserStart($request);
        return new WorkShopCollection($workshop);
    }
}
